<?php
require_once 'phing/Task.php';
require_once 'phing/tasks/user/tokenizer/PhpTokenizer.php';

/**
 * Task strip comments of php source files
 *
 * @author ZhiQiang,He <developerworks@163.com>
 * @package phing.tasks.user
 * @since 2.4.1
 */
class PhpStripCommentsTask extends Task
{
    /**
     * the source files
     *
     * @var  FileSet
     */
    protected $filesets    = array();
    /**
     * Whether the build should fail, if
     * errors occured
     *
     * @var boolean
     */
    protected $failonerror = false;
    /**
     * directory to put minified javascript files into
     *
     * @var  string
     */
    protected $targetDir;

    /**
     *  Nested creator, adds a set of files (nested fileset attribute).
     */
    public function createFileSet()
    {
        $num = array_push($this->filesets, new FileSet());
        return $this->filesets[$num - 1];
    }

    /**
     * Whether the build should fail, if an error occured.
     *
     * @param boolean $value
     */
    public function setFailonerror($value)
    {
        $this->failonerror = $value;
    }

    /**
     * sets the directory where minified javascript files should be put inot
     *
     * @param  string  $targetDir
     */
    public function setTargetDir($targetDir)
    {
        $this->targetDir = $targetDir;
    }

    /**
     * The init method: Do init steps.
     */
    public function init()
    {
        return true;
    }

    /**
     * Strip comments of php source files.
     */
    public function stripComments($source)
    {
        $tokens = token_get_all(stripslashes($source));
        $commentTypes = array(T_COMMENT, T_ML_COMMENT, T_DOC_COMMENT);
        $output = '';
        foreach($tokens as $token)
        {
            if(is_array($token))
            {
                if(in_array($token[0], $commentTypes))
                {
                    $output .= '';
                }else{
                    $output .= $token[1];
                }
            }else{
                $output .= $token;
            }
        }
        return $output;
    }

    /**
     * The main entry point method.
     */
    public function main()
    {
        foreach ($this->filesets as $fs) {
            try {
                $files    = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
                $fullPath = realpath($fs->getDir($this->project));
                foreach ($files as $file) {
                    try {

                        $srcfile = $fullPath . '/' . $file;
                        $target = $this->targetDir . '/' . str_replace($fullPath, '', $file);
                        if (file_exists(dirname($target)) === false) {
                            mkdir(dirname($target), 0700, true);
                        }
                        if(file_exists($target))
                        {
                            if(filemtime($srcfile) > filemtime($target))
                            {
                                $this->log('Updating exists file ' . $file);
                                $source = file_get_contents($srcfile);
                                $striped = PhpTokenizer::strip($source);
                                file_put_contents($target, $striped);
                            }else{
                                //$this->log('Not modified -> ' . $file);
                            }
                        }else{
                            $this->log('Striping comments of file ' . $file);
                            $source = file_get_contents($srcfile);
                            $striped = PhpTokenizer::strip($source);
                            file_put_contents($target, $striped);
                        }
                    } catch (JSMinException $jsme) {
                        $this->log("Could not pack file $file: " . $jsme->getMessage(), Project::MSG_ERR);
                    }
                }
            } catch (BuildException $be) {
                // directory doesn't exist or is not readable
                if ($this->failonerror) {
                    throw $be;
                } else {
                    $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
                }
            }
        }
    }
}
?>